package com.example.ecommerce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ecommerce.entity.ProductSpec;
import com.example.ecommerce.mapper.ProductSpecMapper;
import com.example.ecommerce.service.ProductSpecService;
import com.example.ecommerce.dto.ProductSpecDTO;
import com.example.ecommerce.service.ProductService;
import com.example.ecommerce.exception.BusinessException;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements ProductSpecService {

    @Autowired
    private ProductSpecMapper productSpecMapper;
    
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public List<ProductSpecDTO> getSpecsByProductId(Long productId) {
        // 直接通过Mapper查询商品是否存在
        if (!productSpecMapper.existsProduct(productId)) {
            throw new BusinessException("商品不存在");
        }
        
        // 查询商品规格
        List<ProductSpec> specs = list(new LambdaQueryWrapper<ProductSpec>()
                .eq(ProductSpec::getProductId, productId)
                .orderByAsc(ProductSpec::getSort));
                
        return specs.stream()
                .map(spec -> modelMapper.map(spec, ProductSpecDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public ProductSpecDTO createSpec(ProductSpecDTO specDTO) {
        // 验证商品是否存在
        if (!productSpecMapper.existsProduct(specDTO.getProductId())) {
            throw new BusinessException("商品不存在");
        }
        
        // 验证SKU是否重复
//        if (checkSkuExists(specDTO.getSku(), null)) {
//            throw new BusinessException("SKU已存在");
//        }
        
        ProductSpec spec = modelMapper.map(specDTO, ProductSpec.class);
        spec.setCreatedAt(LocalDateTime.now());
        spec.setUpdatedAt(LocalDateTime.now());
        
        save(spec);
        
        return modelMapper.map(spec, ProductSpecDTO.class);
    }

    @Override
    public ProductSpecDTO updateSpec(Long id, ProductSpecDTO specDTO) {
        ProductSpec existingSpec = getById(id);
        if (existingSpec == null) {
            throw new BusinessException("规格不存在");
        }
        
        // 验证SKU是否重复
//        if (!existingSpec.getSku().equals(specDTO.getSku()) &&
//                checkSkuExists(specDTO.getSku(), id)) {
//            throw new BusinessException("SKU已存在");
//        }
        
        ProductSpec spec = modelMapper.map(specDTO, ProductSpec.class);
        spec.setId(id);
        spec.setUpdatedAt(LocalDateTime.now());
        
        updateById(spec);
        
        return modelMapper.map(spec, ProductSpecDTO.class);
    }

    @Override
    public void deleteSpec(Long id) {
        if (!exists(id)) {
            throw new BusinessException("规格不存在");
        }
        
        removeById(id);
    }

    public boolean exists(Long id) {
        return getById(id) != null;
    }

    @Override
    public void deleteByProductId(Long productId) {
        // 验证商品是否存在
        if (!productSpecMapper.existsProduct(productId)) {
            throw new BusinessException("商品不存在");
        }
        
        // 删除商品的所有规格
        remove(new LambdaQueryWrapper<ProductSpec>()
                .eq(ProductSpec::getProductId, productId));
    }

//    private boolean checkSkuExists(String sku, Long excludeId) {
//        LambdaQueryWrapper<ProductSpec> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ProductSpec::getSku, sku);
//        if (excludeId != null) {
//            queryWrapper.ne(ProductSpec::getId, excludeId);
//        }
//        return count(queryWrapper) > 0;
//    }
} 